//===============================================================================
// Microsoft patterns & practices
// Guidance Explorer Offline Client
//===============================================================================
// Copyright  Microsoft Corporation.  All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE.
//===============================================================================



using System;
using System.Windows.Forms;
using GuidanceExplorer.Model;

namespace GuidanceExplorer.Controls
{
    public abstract class ModelNode<T> : TreeNode, IModelItem
    {
        /// <summary>
        /// The instance in the model for this node.
        /// </summary>
        private T _modelInstance;

        /// <summary>
        /// The latest selected item
        /// </summary>
        private string _selectedItem;

        /// <summary>
        /// Default constructor for the TreeNode.
        /// </summary>
        /// <param name="text">The text of the node.</param>
        /// <param name="imageIndex">The image index.</param>
        /// <param name="selectedImageIndex">The selected image index.</param>
        protected ModelNode(string text, int imageIndex, int selectedImageIndex) : base(text, imageIndex, selectedImageIndex) { }

        /// <summary>
        /// Refers to the instance in the mode.
        /// </summary>
        public virtual T ModelInstance
        {
            get { return _modelInstance; }
            set { _modelInstance = value; }
        }

        #region IModelNode Members

        /// <summary>
        /// Whether the node is editable.
        /// </summary>
        public virtual bool IsEditable
        {
            get { return true; }
            set { }
        }

        /// <summary>
        /// Whether the node can be dropped.
        /// </summary>
        public virtual bool CanDrop(IModelItem modelNode)
        {
            return false;
        }

        #endregion

        /// <summary>
        /// Find the node in the child nodes of the parent.
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="nodeToFind"></param>
        /// <returns></returns>
        public bool FindNodeInChilds(TreeNode parent, TreeNode nodeToFind)
        {
            if (Object.ReferenceEquals(parent, nodeToFind))
            {
                return true;
            }
            else
            {
                foreach (TreeNode child in parent.Nodes)
                {
                    if (Object.ReferenceEquals(child, nodeToFind))
                    {
                        return true;
                    }
                    else
                    {
                        FindNodeInChilds(child, nodeToFind);
                    }
                }
            }
            return false;
        }

        /// <summary>
        /// Gets/Sets the latests selected item
        /// </summary>
        public string SelectedItem
        {
            get { return _selectedItem; }
            set { _selectedItem = value; }
        }
    }
}

